<?php
namespace app\admin\controller;

use app\admin\model\Agent as AgentModel;
use think\Db;
use think\Request;
use think\Cache;

class AgentReport extends Base
{
    public function index()
    {
        $model = new AgentModel;

        //渲染页面模板
        $result = $model->field('a.id,a.code,a.name, a.address,a.created_at,a.registered_at, u.username, d.name as dname, d.sn, r.name as regname')
            ->alias('a')
            ->join('user u','a.user_id = u.id ','left')
            ->join('device d','a.device_id = d.id','left')
            ->join('region r','a.region_id = r.id','left')
            ->order('a.id','desc')
            //->select();
            ->paginate(10);

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('report',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    public function echartsdata(){
        $region = \think\Db::query('SELECT id,p_id,name FROM '. $this->dbTablePrefix .'region');

        $arrregion =Cache::get('arrregion');
        if(empty($arrregion)){
            $arrregion =self::_get_child($region,1);
            foreach($arrregion as $k=>$v){
                foreach($v["child"] as $k2=>$v2){
                    $arrregion[$k]["child"][$k2]["ids"] = [];
                    foreach ($v2["child"] as $k3=>$v3){
                        array_push($arrregion[$k]["child"][$k2]["ids"],$v3["id"]);
                    }
                    unset($arrregion[$k]["child"][$k2]["child"]);
                }
            }

            foreach($arrregion as $k=>$v){
                $arrregion[$k]["ids"] = [];
                foreach($v["child"] as $k2=>$v2){
                    array_push($arrregion[$k]["ids"],$v2["id"]);
                    $arrregion[$k]["ids"] = array_merge($arrregion[$k]["ids"],$v2["ids"]);

                }
                unset($arrregion[$k]["child"]);
                array_push($arrregion[$k]["ids"],$v["id"]);
                unset($arrregion[$k]["id"]);
                unset($arrregion[$k]["p_id"]);
            }
        }
        Cache::set('arrregion',$arrregion);

        $agent = \think\Db::query('SELECT count(id) as countId,region_id FROM '. $this->dbTablePrefix .'franchiser GROUP BY region_id HAVING region_id>0');

        foreach($arrregion as $k=>$v){
            $arrregion[$k]["value"] = "0";
            foreach($agent as $a=>$b){
                if(in_array($b["region_id"],$v["ids"])){
                    $arrregion[$k]["value"] += $b["countId"];
                }
            }
            unset($arrregion[$k]["ids"]);

        }

        if($arrregion){
            return json([
                'code' => 200,
                'data'=>$arrregion
            ]);
        }
    }

    //菜单
    static protected function _get_child($data, $pid=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                //$v['child'] = self::_get_child($data, $v['menu_id']); //递归
                if (!array_key_exists('child', $v)) {//判断$v里面有没有child这个key
                    $v['child'] = self::_get_child($data, $v['id']); //递归
                }
                $array[] = $v;
            }
        }
        return $array;
    }
}